/**
 * controller_leavemsg.js
 * 留言相关控制器
 * 
 * @author 	王中艺	<wangzy_smile@qq.com>
 * @date 	2017-05-10
 */

//课程留言
app.controller('LeaveMsgCourse', function($http, $scope, common){
	$scope.list 	= [];
	$scope.nPage 	= 1;
	$scope.pageHtml 	= '';
	$scope.status 	= false;
	$scope.loading 	= false;

	//初始化JS
	$scope.init_js 	= function(){
		//分页钩子设置
		$('a[data-toggle="tab"]').on('show.bs.tab', function(e){
            var type    = $(this).data('target');
            switch(type){
                case '#list-all'	 : $scope.status   = false; break;
                case '#list-unknown' : $scope.status  = 0; break;
                case '#list-yes' : 	$scope.status   = 1; break;
                case '#list-no' : 	$scope.status   = 2; break;
                default: return false; break;
            }
            $scope.load_data(1);
        });
	}

	//加载数据
	$scope.load_data 	= function(page){
		var page 	= page ? page : 1;
		var url 	= config.prefix_url + '/LeaveMsg/getList?p='+page+
			'&relate_type=1';
		if($scope.status !== false){
			url 	+= '&status='+$scope.status;
		}
		$http.get(url).success(function(info){
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
				return false;
			}
			$scope.list 	= info.data.list;
			$scope.nPage 	= page;
			$scope.pageHtml 	= info.data.page_html;

			common.startPage($scope.load_data);
		})

		$scope.list 	= [];
		$scope.loading 	= true;
	}

	//更新数据
	$scope.update_item 	= function(item, info){
		var url 	= config.prefix_url + '/LeaveMsg/updateItem?';
		var param 	= {
			'msg_id'	: item.id,
			'msg_info'	: info
		}
		
		$http.post(url, param).success(function(info){
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
				return false;
			}
			
			$scope.load_data($scope.nPage);
		})
		$scope.loading 	= true;
	}

	var oldReply 	= '';

	//留言详情
	$scope.detail_item 	= function(item){
		$scope.nItem 	= item;
		oldReply 	= item.reply_cont;
		$('.modal.lmsg-detail').modal('show');
	}

	//回复留言
	$scope.reply_item 	= function(){
		var newReply 	= $scope.nItem.reply_cont;
		if(!newReply || newReply == oldReply){
			$('.modal.lmsg-detail').modal('hide');
			return false;
		}

		var url 	= config.prefix_url + '/LeaveMsg/replyItem';
		var param 	= {
			msg_id 	: $scope.nItem.id,
			content	: newReply
		}

		$http.post(url, param).success(function(info){
			if(info.code != 1){
				common.dealError(info);
			}
			$('.modal.lmsg-detail').modal('hide');

			$scope.loading 	= false;
			$scope.load_data($scope.nPage);
		})

		$scope.loading 	= true;

	}

	$scope.load_data();
	$scope.init_js();
})

//活动留言
app.controller('LeaveMsgActivity', function($http, $scope, common){
	$scope.list 	= [];
	$scope.nPage 	= 1;
	$scope.pageHtml 	= '';
	$scope.status 	= false;
	$scope.loading 	= false;

	//初始化JS
	$scope.init_js 	= function(){
		//分页钩子设置
		$('a[data-toggle="tab"]').on('show.bs.tab', function(e){
            var type    = $(this).data('target');
            switch(type){
                case '#list-all'	 : $scope.status   = false; break;
                case '#list-unknown' : $scope.status  = 0; break;
                case '#list-yes' : 	$scope.status   = 1; break;
                case '#list-no' : 	$scope.status   = 2; break;
                default: return false; break;
            }
            $scope.load_data(1);
        });
	}

	//加载数据
	$scope.load_data 	= function(page){
		var page 	= page ? page : 1;
		var url 	= config.prefix_url + '/LeaveMsg/getList?p='+page+
			'&relate_type=2';
		if($scope.status !== false){
			url 	+= '&status='+$scope.status;
		}
		$http.get(url).success(function(info){
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
				return false;
			}
			$scope.list 	= info.data.list;
			$scope.nPage 	= page;
			$scope.pageHtml 	= info.data.page_html;

			common.startPage($scope.load_data);
		})

		$scope.list 	= [];
		$scope.loading 	= true;
	}

	//更新数据
	$scope.update_item 	= function(item, info){
		var url 	= config.prefix_url + '/LeaveMsg/updateItem?';
		var param 	= {
			'msg_id'	: item.id,
			'msg_info'	: info
		}
		
		$http.post(url, param).success(function(info){
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
				return false;
			}
			
			$scope.load_data($scope.nPage);
		})
		$scope.loading 	= true;
	}

	var oldReply 	= '';
	var oldShowP 	= '';
	//留言详情
	$scope.detail_item 	= function(item){
		$scope.nItem 	= item;
		oldReply 	= item.reply_cont;
		oldShowP 	= item.show_place;
		$('.modal.lmsg-detail').modal('show');
	}

	//回复留言
	$scope.reply_item 	= function(){
		//留言展示位置更改
		var newShowP 	= $scope.nItem.show_place;
		if(oldShowP != newShowP){
			$scope.update_item($scope.nItem, $scope.nItem);
		}
		var newReply 	= $scope.nItem.reply_cont;
		if(!newReply || newReply == oldReply){
			$('.modal.lmsg-detail').modal('hide');
			return false;
		}

		var url 	= config.prefix_url + '/LeaveMsg/replyItem';
		var param 	= {
			msg_id 	: $scope.nItem.id,
			content	: newReply
		}

		$http.post(url, param).success(function(info){
			if(info.code != 1){
				common.dealError(info);
			}
			$('.modal.lmsg-detail').modal('hide');

			$scope.loading 	= false;
			$scope.load_data($scope.nPage);
		})

		$scope.loading 	= true;

	}

	$scope.load_data();
	$scope.init_js();
})
